package com.worldtolearn.user.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.worldtolearn.logging.SingletonLogger;
import com.worldtolearn.user.entity.Account;

@Repository
public class AccountDAO extends HibernateDaoSupport implements IAccountDAO {

	@Autowired
	public void init(SessionFactory factory) {
		setSessionFactory(factory);
	}

	@Override
	public void create(Account account) {	
		getHibernateTemplate().save(account);
		SingletonLogger.monLogger.info("The account has been successfully created!");
	}

	@Override
	public Account read(int id) {
		// TODO VERIFIER LE RETOUR DE LA REQUETE
		String accountQuery = "from ACCOUNT where ACCOUNT_ID = ";
		Account foundAccount = (Account) getHibernateTemplate().find(accountQuery + id);
		return foundAccount;
	}

	@Override
	public void update(Account account) {
		getHibernateTemplate().update(account);
		SingletonLogger.monLogger.info("The account has been successfully modified!");
	}

	@Override
	public void delete(Account account) {
		getHibernateTemplate().delete(account);
		SingletonLogger.monLogger.info("The account has been successfully deleted!");
	}

}
